//
//  LTLgongJu.m
//  WeiBo
//
//  Created by LiTaiLiang on 16/10/18.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLgongJu.h"

@interface LTLgongJu ()

@property (nonatomic , strong ) UIButton *Emontion;

@end

@implementation LTLgongJu

-(instancetype)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]) != nil) {
        //设置子控件
        [self setUI];
//        self.backgroundColor = [UIColor redColor];
    }
    return self;
}
-(void)setUI
{
    /**
     *   LTLComposeToolbarButtonTypeCamera = 0,//拍照
     LTLComposeToolbarButtonTypePicture,//相册
     LTLComposeToolbarButtonTypeMention,//@
     LTLComposeToolbarButtonTypeTrend,//#
     LTLComposeToolbarButtonTypeEmontion,//表情

     */
    // 1.创建5个按钮
    [self setupBtn:@"compose_toolbar_picture" highImage:@"compose_toolbar_picture_highlighted" teg:LTLComposeToolbarButtonTypePicture];
    
    [self setupBtn:@"compose_mentionbutton_background" highImage:@"compose_mentionbutton_background_highlighted" teg:LTLComposeToolbarButtonTypeMention];
    
    [self setupBtn:@"compose_trendbutton_background" highImage:@"compose_trendbutton_background_highlighted" teg:LTLComposeToolbarButtonTypeTrend];
    
    [self setupBtn:@"compose_emoticonbutton_background" highImage:@"compose_emoticonbutton_background_highlighted" teg:LTLComposeToolbarButtonTypeEmontion];
    
    [self setupBtn:@"compose_camerabutton_background" highImage:@"compose_camerabutton_background_highlighted" teg:LTLComposeToolbarButtonTypeCamera];
    

}

//创建按钮
- (void)setupBtn:(NSString *)image highImage:(NSString *)highImage teg:(NSInteger)teg
{
    // 1.创建按钮
    UIButton *btn = [[UIButton alloc] init];
    btn.tag = teg;
    if (teg == LTLComposeToolbarButtonTypeEmontion) {
        self.Emontion = btn;
    }
    // 2.设置按钮默认状态图片
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    // 3.设置按钮高亮状态图片
    [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    // 4.监听按钮的点击
    
    [btn addTarget:self action:@selector(didButtom:) forControlEvents:UIControlEventTouchUpInside];
    // 5.将按钮添加到view中
    [self addSubview:btn];
    
}
//布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    // 2.调整5个按钮的frame
    NSUInteger count = self.subviews.count;
    CGFloat width = self.width / count;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.subviews[i];
        btn.highly = self.highly;
        btn.width = width;
        btn.y = 0;
        btn.x = i * width;
    }
}
-(void)didButtom:(UIButton *)btn
{
    
    //CGFloat btnX = btn.x;
    
    LTLComposeToolbarButtonType num =btn.tag;
    //NSLog(@"%ld",num);
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(composeToobar:didClickBtn:)]) {
        [self.delegate composeToobar:self didClickBtn:num];
    }
}

-(void)setKeyExpression:(BOOL)keyExpression
{
    _keyExpression = keyExpression;
    
    if (_keyExpression) {
        [self.Emontion setImage:[UIImage imageNamed:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
        [self.Emontion setImage:[UIImage imageNamed:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];
    } else {
        [self.Emontion setImage:[UIImage imageNamed:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
        [self.Emontion setImage:[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
    }
}


@end
